package com.kamistoat.meimeimall.meimeimallware.service.impl;

import com.kamistoat.meimeimall.common.utils.PageUtils;
import com.kamistoat.meimeimall.common.utils.Query;
import com.kamistoat.meimeimall.meimeimallware.dao.PurchaseDetailDao;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.kamistoat.meimeimall.meimeimallware.entity.PurchaseDetailEntity;
import com.kamistoat.meimeimall.meimeimallware.service.PurchaseDetailService;
import org.springframework.util.StringUtils;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                new QueryWrapper<PurchaseDetailEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 自己方法，查采购需求
     *
     * @param params 查询条件：仓库id、状态、key
     * @return 分页封装
     */
    @Override
    public PageUtils queryPageByPurchaseDetail(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(params.get("status"))){
            wrapper.eq("status",params.get("status"));
        }
        if(!StringUtils.isEmpty(params.get("wareId"))){
            wrapper.eq("ware_id",params.get("wareId"));
        }
        if(!StringUtils.isEmpty(params.get("key"))){
            wrapper.and(obj->{
                obj.eq("purchase_id",params.get("key")).or().eq("sku_id",params.get("key"));
            });
        }
        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

}